/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-10-07 17:03:59
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-10-07 17:05:53
 * @FilePath: \客户端\webpack\app.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const WebSocket = require('ws');

//建立后端服务器
let ws = new WebSocket.Server({
    port: 8888  //自定义配置端口号
})

let userList = []; //定义保存用户的数组

//连接 
ws.on('connection', (socket) => { //socket:当前客户端

    userList.push(socket); //将当前用户添加到数组中
    //监听消息
    socket.on('message', (msg) => {

        //遍历用户列表 ==> 发送给当前客服端(socket)之外的客户端
        userList.forEach(item => {
            if (item !== socket) {
                item.send(msg);
            }
        })
    })
})
